using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExternalClasses.Util
{
    /// <summary>
    /// Quick but not efficient implementation of priority queue
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class PriorityQueue<T> : IEnumerable<T>
    {
        protected List<T> queue;

        IComparer<T> comparer;

        public int Count
        {
            get { return this.queue.Count; }
        }

        public PriorityQueue(int capacity, IComparer<T> comparer)
        {
            queue = new List<T>(capacity);
            this.comparer = comparer;
        }

        public void Enqueue(T item)
        {
            this.queue.Add(item);
            this.queue.Sort(comparer);
        }

        public T DequeueMin()
        {
            return Dequeue(0);
        }

        public T DequeueMax()
        {
            return Dequeue(this.Count - 1);
        }

        private T Dequeue(int index)
        {
            this.queue.Sort(comparer);
            T val = this.queue[index];
            this.queue.RemoveAt(index);
            return val;
        }

public void EnqueueRange(IEnumerable<T> values)
        {
            this.queue.AddRange(values);
            this.queue.Sort(comparer);
        }

        public IEnumerator<T> GetEnumerator()
        {
            return queue.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return queue.GetEnumerator();
        }
    }
}
